package newapp.dob.com.dc_new_app.util;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Handler;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import newapp.dob.com.dc_new_app.R;


/**
 * 自定义对话框
 * Created by Master.Dong on 2016/4/13.
 */
public class Dialog {




    /**
     * 显示无标题的默认按钮
     *
     * @param context             上下文
     * @param content             内容
     * @param leftButton          左边按钮
     * @param rightButton         右边按钮
     * @param dialogClickListener 点击事件回调
     * @return
     */
    public static android.app.Dialog showDefaultDialog(Context context, String title, String content,
                                                       String leftButton, String rightButton, boolean showCloseBtn,
                                                       final DialogDefaultClickListener dialogClickListener) {
        final android.app.Dialog dialog = new android.app.Dialog(context,
                R.style.DialogStyle);
        View view = LayoutInflater.from(context)
                .inflate(R.layout.default_dialog, null);
        dialog.setContentView(view);
        LinearLayout titleView = (LinearLayout) view.findViewById(R.id.title_view);
        TextView mTitle = (TextView) view.findViewById(R.id.title);
        TextView mContext = (TextView) view.findViewById(R.id.content);
        TextView mLeftBtn = (TextView) view.findViewById(R.id.left_button);
        TextView mClose = (TextView) view.findViewById(R.id.close);
        if (showCloseBtn) mClose.setVisibility(View.VISIBLE);
        else mClose.setVisibility(View.INVISIBLE);

        //标题
        if (TextUtils.isEmpty(title))
            mTitle.setVisibility(View.GONE);
        else {
            titleView.setVisibility(View.VISIBLE);
            mTitle.setText(title);
        }
        //内容
        if (TextUtils.isEmpty(content)) {
            mContext.setVisibility(View.GONE);
            ((View) view.findViewById(R.id.line)).setVisibility(View.VISIBLE);
        } else {
            mContext.setVisibility(View.VISIBLE);
            mContext.setText(content);
        }

        //左边按钮
        if (TextUtils.isEmpty(leftButton)) {
            mLeftBtn.setVisibility(View.GONE);
            ((View) view.findViewById(R.id.d_line)).setVisibility(View.GONE);
        } else {
            mLeftBtn.setVisibility(View.VISIBLE);
            mLeftBtn.setText(leftButton);
        }
        ((TextView) view.findViewById(R.id.right_button)).setText(rightButton);
        dialog.setCancelable(false);
        view.findViewById(R.id.left_button).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                        dialog.dismiss();
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                dialogClickListener.cancel();
                            }
                        }, 200);
                    }
                });
        view.findViewById(R.id.right_button).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                        dialog.dismiss();
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                dialogClickListener.confirm();
                            }
                        }, 200);
                    }
                });
        mClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        Window mWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {// 横屏
            lp.width = Util.getDisplayMetrics(context).heightPixels / 10 * 8;
        } else {
            lp.width = Util.getDisplayMetrics(context).widthPixels / 10 * 8;
        }
        mWindow.setAttributes(lp);
        dialog.show();
        return dialog;
    }











    public interface DialogClickListener {
        public abstract void confirm();

    }

    public interface DialogDefaultClickListener {
        public abstract void confirm();

        public abstract void cancel();
    }

    public interface DialogSystemDefaultClickListener {
        public abstract void confirm();

        public abstract void cancel();

        public abstract void close();
    }


    public interface IdCardClickListener {
        public abstract void confirm(String name, String address);

        public abstract void cancel();
    }


}
